const fs = require('fs')
const uuid = require("../utils/uuid");
var msg = require('../utils/message');
const { select } = require('../db/mysql');
const getUserById = (req, res, next) => {
  let sql = 'select * from user';
  return select(sql)
}
const uploadpostimg = (req, res) => {
  // console.log(req.file)
  if (req.file == undefined) {
    res.send(msg(400, [], '上传文件不能为空！'));
  } else {
    let file = req.file;
    let fileInfo = {};
    fs.renameSync('./public/images/' + file.filename, './public/images/' + file.originalname);//这里修改文件名字，比较随意。
    fileInfo.mimetype = file.mimetype;
    fileInfo.originalname = file.originalname;
    fileInfo.size = file.size;
    fileInfo.path = file.path;
    res.set({
      'content-type': 'application/json; charset=utf-8'
    });
    let image_id = uuid(16);
    let imgUrl = 'http://localhost:8080/public/images/' + file.originalname;
    let sql = `INSERT INTO postimage(image_id,image_url,file_name) VALUES ('${image_id}','${imgUrl}','${fileInfo.originalname}')`;
    select(sql).then((data) => {
      console.log("data", data)
      if (data != '') {
        res.send(msg(200, fileInfo, '上传成功'))
      } else {
        res.send(msg(400, [], '上传失败'))
      }
    })
  }
}
module.exports = {
  getUserById, uploadpostimg
}